<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletes;

class Supplier extends Base
{
    use SoftDeletes;

    const NATURE_FACTORY = 1;

    const NATURE_DISTRIBUTOR = 2;

    const NATURE_MAP = [
        self::NATURE_FACTORY => '工厂',
        self::NATURE_DISTRIBUTOR => '经销商',
    ];

    const SETTLEMENT_METHOD_ONLINE = 1;

    const SETTLEMENT_METHOD_OFFLINE = 2;

    const SETTLEMENT_METHOD_MAP = [
        self::SETTLEMENT_METHOD_ONLINE => '在线',
        self::SETTLEMENT_METHOD_OFFLINE => '线下',
    ];

    const SUPPORT_INVOICING_TRUE = 1;

    const SUPPORT_INVOICING_FALSE = 2;

    const SUPPORT_INVOICING_MAP = [
        self::SUPPORT_INVOICING_TRUE => '是',
        self::SUPPORT_INVOICING_FALSE => '否',
    ];

    const STATUS_MAP = [
        self::STATUS_NORMAL => '启用',
        self::STATUS_DISABLE => '禁用'
    ];

    protected $hidden = [
        'password',
    ];

    public function getNatureAttribute($value)
    {
        return self::NATURE_MAP[$value];
    }

    public function getSettlementMethodAttribute($value)
    {
        return self::SETTLEMENT_METHOD_MAP[$value];
    }

    public function getSupportInvoicingAttribute($value)
    {
        return self::SUPPORT_INVOICING_MAP[$value];
    }

    public function province()
    {
        return $this->hasOne(Province::class, 'code', 'province');
    }

    public function city()
    {
        return $this->hasOne(City::class, 'code', 'city');
    }

    public function area()
    {
        return $this->hasOne(Area::class, 'code', 'area');
    }

    public function returnProvince()
    {
        return $this->hasOne(Province::class, 'code', 'return_province');
    }

    public function returnCity()
    {
        return $this->hasOne(City::class, 'code', 'return_city');
    }

    public function returnArea()
    {
        return $this->hasOne(Area::class, 'code', 'return_area');
    }

    /**
     * 自定义操作日志 说明
     * @param $eventName
     * @return string
     */
    public function getDescriptionForEvent(string $eventName): string
    {
        if (!in_array($eventName, ['created', 'updated', 'deleted'])) {
            return $eventName;
        }

        switch ($eventName) {
            case 'created':
                return '创建供应商:' . $this->name;
            case 'updated':
                $fieldMap = [
                    'name' => [
                        'title' => '供应商名称',
                    ],
                ];
                $changes = [];
                foreach ($this->getChanges() as $field => $change) {
                    if (!isset($fieldMap[$field])) {
                        continue;
                    }

                    $changes[] = $fieldMap[$field]['title'] . '变更为' . (isset($fieldMap[$field]['casts']) ? $fieldMap[$field]['casts'][$change] : $change);
                }
                return '编辑供应商:' . $this->getOriginal('name') . '; 变更内容:' . implode(',', $changes);
            case 'deleted':
                return '删除供应商:' . $this->name;
            default:
                return $eventName;
        }
    }
}
